<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

if (!($tip_id = $route->getAdminId()))
	goToPage("/admin/tips");

$errors = array();

if ($route->POST("action") == 'edit') {

	$section = UV::readInt($route->POST("section"));
	$title = UV::readMQ($route->POST("title"));
	$link = translitIt(strip_tags($title));
	$i = 1;
	while ($db->getCell("select `link` from `tips` where `link` = '$link' and `ts_id` = '$section' and `id` != '$tip_id' limit 1")){
		$link = translitIt(strip_tags($title))."-".($i++);
	}
	$text = UV::readMQ($route->POST("text"));

	if (count($errors) == 0) {

		$query = "update `tips` set `ts_id` = '$section',`title`='$title',`link`='$link',`text`='$text' where `id` = '$tip_id' limit 1";
		$update = $db->sendQuery($query);
		if (!$update){
			$errors[] = "Ошибка обновления записи";
		}
	}
}

$tip = $db->getRow("select * from `tips` where `id` = '$tip_id' limit 1");
if (!$tip)
	goToPage("/admin/tips");

loadPage("head.php");
?>
<h1>Редактирование статьи</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
	elseif ($route->POST("action") == 'edit')
		echo "<p class='success'>Успешно отредактировано<br /><br /></p>";
?>
<form method='POST' id="edit">
	<input type='hidden' name='action' value='edit' />
	<table cellpadding='3' cellspacing='5'>
		<tr>
			<td width='170px'><label for='section'>Раздел</label></td>
			<td width='600px'>
				<select name='section' class="double_short" id='section'><?php
					$sections = $db->getTable("select `id`,`name` from `tip_sections` order by `name`");
					echo select_create($sections,"id","name",$tip['ts_id']);
				?></select>
			</td>
		</tr>
		<tr>
			<td><label for='title'>Заголовок</label></td>
			<td><input type='text' name='title' id='title' value="<?php echo htmlspecialchars($tip['title']); ?>" /></td>
		</tr>
		<tr>
			<td><label for='text'>Текст</label></td>
			<td><textarea name='text' id='text'><?php echo $tip['text']; ?></textarea></td>
		</tr>
		<tr>
			<td colspan='2' align='right'><input type='submit' value='Редактировать' /></td>
		</tr>
	</table>
</form>

<?php loadPage("footer.php"); ?>